﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data.SqlClient;
using Infrastructure.Aspect.ExceptionHandling;
namespace SkynetWebServices
{
    /// <summary>
    /// Summary description for SkynetImageHandler
    /// </summary>
    public class SkynetImageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            var imgId = context.Request.QueryString["id"];
            Guid Id;
            if (!Guid.TryParse(imgId, out  Id)) return;
            Bitmap b = context.Cache[imgId] as Bitmap;
            ImageFormat format = ImageFormat.Jpeg;
            if (b == null)
            {
                b = GetImage(Id, out format);
                context.Cache.Insert(imgId, b);
            }
            if (b != null)
            {                
                context.Response.ContentType = "image/jpeg";
                b.Save(context.Response.OutputStream, format);                       
            }            
        }
        private Bitmap GetImage(Guid id, out ImageFormat format)
        {
            Bitmap b = null;
            SqlConnection con = null;
            format = null;
            try
            {
                var str = System.Configuration.ConfigurationManager.ConnectionStrings["SkyNetLibrary"].ConnectionString;
                con = new SqlConnection(str);
                var cmd = con.CreateCommand();
                cmd.CommandText = "usp_Images_getone";
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Id", id);
                con.Open();
                var reader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
                int imgIndex = reader.GetOrdinal("Image");
                int imgtypeIndex = reader.GetOrdinal("ImageType");
                while (reader.Read())
                {
                    var bits = reader.GetSqlBytes(imgIndex);
                    switch (reader.GetString(imgtypeIndex).ToLower())
                    {
                        case "jpeg":
                            format = ImageFormat.Jpeg;
                            break;
                        default:
                            return null;
                    }                    
                    b = new Bitmap(bits.Stream);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                bool rethrow = Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex, ExceptionPolicyNames.ServiceInterfacePolicy);
                if (rethrow)
                    throw;                
            }
            finally
            {
                if (con != null) con.Close();
            }
            return b;
        }
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}